<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;


class LottoryTicketLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $lottoryTicketModel;

    public function _initialize()
    {
        $this->lottoryTicketModel = D('LottoryTicket', 'Model');
    }

    /**
     * 获取开奖活动对应的奖券列表
     *
     * @param $page
     * @param $pageSize
     * @param $params
     *
     * @return array
     */
    public  function getLottoryTicketList($page, $pageSize, $params)
    {
        $page = $page < 1 ? 1 : (int)$page;
        $pageSize = $pageSize < 1 ? 10 : (int)$pageSize;
        $map = [];
        /**
         * 搜索条件：
         * 1、奖券ID 2、奖券类型 3、个人ID 4、帐号 5、邀请人ID
         * 6、开奖情况：1、已中奖 2、未中奖 7、开始时间 8、结束时间
         */
        if ($params['lot_id']) {
            $map['lt.lot_id'] = ['eq', $params['lot_id']];
        }
        if ($params['lt_num']) {
            $lt_num = preg_replace('/^0+/', '', trim($params['lt_num']));
            $map['lt.lottory_ticket_num'] = ['LIKE', '%' . $lt_num . '%'];
        }
        if ($params['lt_type']) {
            $map['lt.lottory_ticket_comefrom'] = ['eq', $params['lt_type']];
        }
        if ($params['lt_uid']) {
            $map['lt.uid'] = ['LIKE', '%' . $params['lt_uid'] . '%'];
        }
        if ($params['lt_phone']) {
            $phone = trim($params['lt_phone']);
            $info = D('ClcwUser', 'Logic')->getUserInfoByMobile($phone);
            if (!empty($info)) {
                $map['lt.uid'] = ['eq', $info['uid']];
            } else {
                $map['lt.uid'] = ['eq', 0];
            }
        }
        if ($params['invite_uid']) {
            $map['lt.lottory_ticket_comefrom'] = ['eq', 3];
            $map['lt.invite_uid'] = ['LIKE', '%' . $params['invite_uid'] . '%'];
        }
        if ($params['lt_status']) {
            $lt_status = $params['lt_status'];
            if ($lt_status == 1) {
                $map['lt.lottory_ticket_status'] = ['eq', $params['lt_status']];
            } else {
                $map['lt.lottory_ticket_status'] = ['eq', $params['lt_status']];
            }
        }
        if ($params['start_time']) {
            $map['lt.create_time'] = ['egt', strtotime($params['start_time'] . ' 00:00:00')];
        }
        if ($params['end_time']) {
            $map['lt.create_time'] = ['elt', strtotime($params['end_time'] . ' 23:59:59')];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['lt.create_time'] = [
                ['egt', strtotime($params['start_time'] . ' 00:00:00')],
                ['elt', strtotime($params['end_time'] . ' 23:59:59')]
            ];
        }
        $cols = "l.lottory_win_num,
                 lt.id,lt.lot_id,lt.uid,lt.invite_uid,lt.lottory_ticket_num,lt.lottory_ticket_comefrom,
                 lt.lottory_ticket_status,lt.create_time";
        $order = "lt.id DESC";
        $total = $this->lottoryTicketModel->alias('lt')->where($map)->count();
        $list = $this->lottoryTicketModel->alias('lt')->field($cols)
            ->join('clcw_lottory as l ON lt.lot_id=l.id')
            ->where($map)
            ->order($order)
            ->page($page)
            ->limit($pageSize)
            ->select();
        foreach ($list as $k => $v) {
            $info = D('ClcwUser', 'Logic')->getClcwUserInfo($v['uid']);
            $list[$k]['user'] = $info['mobile'];
        }
        $result = [
            'list'  => $list,
            'total' => $total,
        ];
        return $result;
    }



    /**
     * 获取活动的奖券数量
     *
     * @param int $lotId
     *
     * @return bool
     */
    public function getLottoryTicketNum($lotId = 0)
    {
        if ($lotId < 1) {
            return false;
        }
        $condition['lot_id'] = $lotId;
        return $this->lottoryTicketModel->where($condition)->count();
    }

}